app.controller('riskManagementCtrl',['$scope','$rootScope','$http','cookies','myHttp','pcdService','$filter','listSve',function($scope,$rootScope,$http,cookies,myHttp,pcdService,$filter,listSve){
	
	
	//$rootScope.userName = cookies.getCookie('userName'); //获取cookie用户名
	$rootScope.navIndex = 3;

	angular.extend($scope,{

		//初始化
		init: function(){
			$scope._roomName = '';  //搜索用户名字
			$scope._roomPhone = '';  //搜索用户电话
			$scope.listData = {};  //页面列表数据
			$scope.iMax = 20;
			$scope.iMax2 =20;
			$scope.isFirst = true;

			//获取省列表
			pcdService.prolist().list().then(function(e) {
				$scope.prolist = e.data;
			})
			pcdService.citylist().list().then(function(e) {
				$scope.oCityList = e.data;
			})
			pcdService.dislist().list().then(function(e) {
				$scope.oDisList = e.data;
			})

			//初始化请求
			listSve.riskManagementList().list({numPerPage:20}).then(function(data) {

				console.log(data);
				$scope.listData = data;

				//创建分页
				$scope.iMax = Math.ceil(860/20);
				$scope.doPager('pager_1',$scope.iMax,$scope.doChangePage); //参数分别为：容器，分页数，分页执行方法

			});
		},

		//生成分页
		doPager: function(box,maxNum,pageFn){

			var pagerBox = document.getElementById(box);
			pagerBox.innerHTML = '';
			var pager = new Pager({
				index: 1,
				total: maxNum,
				parent: pagerBox,
				onchange: pageFn
			});

		},

		//分页执行事件
		doChangePage: function(obj){

			if($scope.isFirst){

				$scope.isFirst = false;

			} else{

				var _index = obj.index;

				listSve.riskManagementList().list({pageNum: _index,numPerPage:20}).then(function(data) {

					console.log(data);
					$scope.listData = data;

				});


			}

		},

		//搜索
		search: function(){
if($scope._roomPhone == '' && $scope._districtName == ''){
				//$scope.alert({show: true, Title:'系统提示', Message:'手机号不能为空', prevId:'',Type:false});
				//初始化请求
				$scope.isFirst = true;
				listSve.riskManagementList().list({districtName:$scope._districtName,numPerPage:20}).then(function(data) {

					console.log(data);
					$scope.listData = data;

					//创建分页
					$scope.iMax = Math.ceil(860/20);
					$scope.doPager('pager_1',$scope.iMax,$scope.doChangePage); //参数分别为：容器，分页数，分页执行方法

				});
			}else if($scope._roomPhone == '' && !$scope._districtName == ''){
				$scope.isFirst = true;
				listSve.riskManagementList().list({districtName:$scope._districtName,numPerPage:100}).then(function(data) {

					console.log(data);
					$scope.listData = data;

					//创建分页
					$scope.iMax = Math.ceil(20/20);
					$scope.doPager('pager_1',$scope.iMax,$scope.doChangePage); //参数分别为：容器，分页数，分页执行方法

				});
			}else{


				$scope.isFirst = true;

				listSve.riskManagementList().list({mobile: $scope._roomPhone,districtName:$scope._districtName,numPerPage:20}).then(function(data) {

					console.log(data)
					$scope.listData = data;

					//创建分页
					$scope.iMax = Math.ceil(1/20);
					$scope.doPager('pager_1',$scope.iMax,$scope.doChangePage);

				});



			}



		},

	

		//设备状态
		status: function(x,n){

			for(var i=0;i<x.hardwares.length;i++){

				if(x.hardwares[i].type == n){

					var className = '';
					var s = x.hardwares[i].hardwareStatus;

					if(s == 'ZC'){

						return className += 'ZC';

					} else if(s == 'YC'){

						return className += 'YC';

					} else if(s == 'WZC'){

						return className += 'WZC';

					} else if(s == 'SD'){

						return className += 'SD';

					}

					return className;

				};

			}

		}

	}).init();

	//电量历史
	$scope.power = function(e,d){
		$scope.chart = true;
		$('#modal_5').modal('hide')
		$('#modal_5').one('hidden.bs.modal', function (e) {
			$('#modal_5_1').modal('show')
		})
		document.getElementById('chart').innerHTML = '';
		document.getElementById('chart_1').innerHTML = '';
		//powerList
		/*$scope.power = {};
		 $scope.power.devId = e;
		 $scope.power.type = 'week';*/
		$scope.powerObj = {
			devId:e,
			type:'week'
		}
		listSve.powerList().list($scope.powerObj).then(function(e) {
			var txt = d;
			console.log(e);
			//电量列表
			$scope.power_list = e.res.data.data;
			var dataName = e.res.data.data;
			var oDataName = [];
			var oData = [];
			for (var i=0;i<dataName.length;i++) {
				oDataName.unshift(dataName[i].Date)
//				oData.unshift(dataName[i].Allpower)
				oData.unshift(Math.floor((dataName[i].Allpower) * 100) / 100)
			}
			console.info(oDataName)
			// 基于准备好的dom，初始化echarts实例
			var myChart = echarts.init(document.getElementById('chart'));
			// 指定图表的配置项和数据
			var option = {
				title: {
					text: txt,
					subtext: '单位 ：度'
				},
				tooltip: {
					trigger: 'axis'
				},
				xAxis: {
					data: oDataName
				},
				yAxis: {},
				series: [{
					name: '电量（度）',
					type: 'line',
					data: oData
				}]
			};
			// 使用刚指定的配置项和数据显示图表。
			myChart.setOption(option);
		})

		//月用电量数据---------------------------------------//
		$scope.power_1 = {};
		$scope.power_1.devId = e;
		$scope.power_1.type = 'month';
		listSve.powerList().list($scope.power_1).then(function(e) {
			var txt = d;
			//月电量列表
			$scope.power_list_1 = e.res.data.data;
			console.log(e);
			var dataName = e.res.data.data;
			var oDataName = [];
			var oData = [];
			for (var i=0;i<dataName.length;i++) {
				oDataName.unshift(dataName[i].Date)
//				oData.unshift(dataName[i].Allpower)
				oData.unshift(Math.floor((dataName[i].Allpower) * 100) / 100)
			}
			console.info(oDataName)
			console.log(oData)
			// 基于准备好的dom，初始化echarts实例
			var myChart = echarts.init(document.getElementById('chart_1'));
			// 指定图表的配置项和数据
			var option = {
				title: {
					text: txt,
					subtext: '单位 ：度'
				},
				tooltip: {
					trigger: 'axis'
				},
				xAxis: {
					data: oDataName
				},
				yAxis: {},
				series: [{
					name: '电量（度）',
					type: 'line',
					data: oData
				}]
			};
			// 使用刚指定的配置项和数据显示图表。
			myChart.setOption(option);
		})
	}

	$scope.doorModal = function(o,h){
		console.info(h)
		if (o.type == "DIANBIAO") {
			$scope.mdl = '#modal_5';
			$scope.warsName = '电表';
			$scope.supname = o.ammeter.supplier.name;
			$scope.devId = o.ammeter.devId;
			//$scope.hardwdress =$filter('proName')(h.houseStreet.provinceId,$scope.prolist)+$filter('cityName')(h.houseStreet.cityId,$scope.oCityList)+$filter('disName')(h.houseStreet.dist,$scope.oDisList)+h.houseStreet.street;
//			$scope.hardwdress = $filter('proName')(h.houseStreet.provinceId, $scope.prolist) + $filter('cityName')(h.houseStreet.cityId, $scope.oCityList) + $filter('disName')(h.houseStreet.dist, $scope.oDisList) + h.houseStreet.street + h.floorNo + "栋" + h.unit + "单元" + h.roomNo + "室" + h.door;
//			$scope.hardwdress2 = h.districtName+'   '+$filter('proName')(h.houseStreet.provinceId, $scope.prolist) + $filter('cityName')(h.houseStreet.cityId, $scope.oCityList) + $filter('disName')(h.houseStreet.dist, $scope.oDisList) + h.houseStreet.street + h.floorNo + "栋" + h.unit + "单元" + h.roomNo + "室" + h.door;
			$scope.hardwdress = h.houseStreet.provinceName + h.houseStreet.cityName + h.houseStreet.distName + h.houseStreet.street + h.floorNo + "栋" + h.unit + "单元" + h.roomNo + "室" + h.door;
			$scope.hardwdress2 = h.districtName+'   '+h.houseStreet.provinceName + h.houseStreet.cityName + h.houseStreet.distName + h.houseStreet.street + h.floorNo + "栋" + h.unit + "单元" + h.roomNo + "室" + h.door;
//			$scope.payMod = o.ammeter.payMod;
//			$scope.install = o.ammeter.installTime
//			$scope.electricityPrice = o.ammeter.electricityPrice;
			$scope.status = {};
			$scope.status.devId =o.ammeter.devId;
			listSve.zStatus().list($scope.status).then(function(e) {
				console.log(e);
				if (e.code == 1) {
					$scope.alert({show: true, Title:'系统提示', Message:"该设备暂无法查询",prevId:'',Type:false});
				} else{
					$('#modal_5').modal('show')
					var oId = o.type+h.id;
					angular.element("#"+oId).attr({'class':e.res.data.hardwareStatus});
					$scope.hardwareStatus_1 = e.res.data.hardwareStatus;
//					$scope.payMod = e.res.data.mode;
//					$scope.electricityPrice = e.res.data.price;
//					$scope.hardwareStatus_2 = e.res.data.td_status;
					if($scope.hardwareStatus_1 == 'ZC'){
						$scope.hardwareStatus_1 = '在线';
						$scope.clor = "";
					} else{
						$scope.clor = "red";
						$scope.hardwareStatus_1 = '离线';
					};
//					if($scope.hardwareStatus_2 == 0){
//						$scope.clor_t = "red";
//					}else{
//						$scope.clor_t = "";
//					};
				}

			})





		}else if(o.type == "MENSUO"){
			$scope.mdl = '#modal_4';
			$scope.warsName = '门锁';
			$scope.supname_1 = o.doorLock.supplier.name;
			$scope.devId_1 = o.doorLock.devId;
//			$filter('proName')(h.houseStreet.provinceId,$scope.prolist)+$filter('cityName')(h.houseStreet.cityId,$scope.oCityList)+$filter('disName')(h.houseStreet.dist,$scope.oDisList)
			//$scope.hardwdress_1 = $filter('proName')(h.houseStreet.provinceId,$scope.prolist)+$filter('cityName')(h.houseStreet.cityId,$scope.oCityList)+$filter('disName')(h.houseStreet.dist,$scope.oDisList)+h.houseStreet.street;
			//$scope.hardwdress_1_1 = h.districtName+$filter('proName')(h.houseStreet.provinceId,$scope.prolist)+$filter('cityName')(h.houseStreet.cityId,$scope.oCityList)+$filter('disName')(h.houseStreet.dist,$scope.oDisList)+h.houseStreet.street+ h.floorNo + '栋' + h.unit + "单元" + h.roomNo + "室" + h.door;
//			$scope.hardwdress_1 = $filter('proName')(h.houseStreet.provinceId, $scope.prolist) + $filter('cityName')(h.houseStreet.cityId, $scope.oCityList) + $filter('disName')(h.houseStreet.dist, $scope.oDisList) + h.houseStreet.street + h.floorNo + '栋' + h.unit + "单元" + h.roomNo + "室" + h.door;
//			$scope.hardwdress_1_1 = h.districtName+'   '+$filter('proName')(h.houseStreet.provinceId, $scope.prolist) + $filter('cityName')(h.houseStreet.cityId, $scope.oCityList) + $filter('disName')(h.houseStreet.dist, $scope.oDisList) + h.houseStreet.street + h.floorNo + '栋' + h.unit + "单元" + h.roomNo + "室" + h.door;
			$scope.hardwdress_1 = h.houseStreet.provinceName + h.houseStreet.cityName + h.houseStreet.distName+ h.houseStreet.street + h.floorNo + '栋' + h.unit + "单元" + h.roomNo + "室" + h.door;
			$scope.hardwdress_1_1 = h.districtName+'   '+h.houseStreet.provinceName + h.houseStreet.cityName + h.houseStreet.distName + h.houseStreet.street + h.floorNo + '栋' + h.unit + "单元" + h.roomNo + "室" + h.door;


			//doorList
			$scope.door = {};
			$scope.door.devId =o.doorLock.devId;
			$scope.door.type = 'MENSUO';
			listSve.doorList().list($scope.door).then(function(e) {
				console.log(e)
				if (e.code == 1) {
//					$scope.alert = {Message:e.res.msg,Title:'系统提示',Type:false}
//					$('#ycf-alert').modal('show')
					$scope.alert({show: true, Title:'系统提示', Message:"该设备暂无法查询",prevId:'',Type:false});
				} else{
					$('#modal_4').modal('show')
					var oId_1 = o.type+h.id;
					$("#"+oId_1).attr({'class':e.res.data.hardwareStatus});
//					$scope.hardwareStatus_4 = e.res.data.electric;
					$scope.hardwareStatus_3 = e.res.data.hardwareStatus;//在线

					if($scope.hardwareStatus_3 == 'ZC'){
						$scope.hardwareStatus_3 = '在线';
						$scope.clor_1 = "";
					} else{
						$scope.hardwareStatus_3 = '离线';
						$scope.clor_1 = "red";
					}

//					if(e.res.data.electric<20){
//						$scope.clor_2 = "red";
//					}else{
//						$scope.clor_2 = "";
//					};
					/*if(e.res.data.success == "false"){
					 $scope.clor_1 = "red";
					 }*/
				}

				
			})
			$scope.install_1 = o.doorLock.installTime

//			if(o.hardwareStatus == "YC"){
//				$scope.clor_1 = "red";
//			}else{
//				$scope.clor_1 = "";
//			};
		}


	};
	//点击切换周月用电详情
	$scope.oWeek = function(){
		$scope.chart = true;
	};
	$scope.oMonth = function(){
		$scope.chart = false;
	};

	//开门记录openDoor
	var pagerBox_1 = document.getElementById('pager_3');
	var pagerBox_1_1 = document.getElementById('pager_3_1');
	$scope.openDoor = function(e,d){
		$scope.history = true;
		//data-dismiss="modal" data-toggle="modal" data-target="#modal_4_1"
		$('#modal_4').modal('hide')
		$('#modal_4').one('hidden.bs.modal', function (e) {
			$('#modal_4_1').modal('show')
		})
		//周开门记录
		$scope.doorClear_1 = {};
		$scope.doorClear_1.devId = e;
		$scope.doorClear_1.numPerPage =5;
		$scope.doorClear_1.type = 'week';
		$scope.openDress = d;
		listSve.doorClear_1().list($scope.doorClear_1).then(function(e) {
			console.info(e)
			$scope.openlist_1 = e.res.data;

		})
		//月开门记录
		$scope.doorClear = {};
		$scope.doorClear.devId = e;
		$scope.doorClear.numPerPage =20;
		$scope.doorClear.type = 'month';
		$scope.openDress = d;
		listSve.doorClear_1().list($scope.doorClear).then(function(e) {
			console.info(e)
			$scope.openlist = e.res.data;
			
		})


	}
	//点击切换周月开门记录
	$scope.oWeek_1 = function(){
		$scope.history = true;
	};
	$scope.oMonth_1 = function(){
		$scope.history = false;
	};
	

	
}])